﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Controls
{
    [DefaultEvent("MySelectButtonClick")]
    public partial class Selecter : UserControl
    {
        protected static readonly object objTextChangetKey = new object();
        protected static readonly object objSelectButtonClick = new object();
        private object _selectedValue;

        public object SelectedValue
        {
            get { return _selectedValue; }
            set { _selectedValue = value; }
        }

        public string SelectedText
        {
            get
            {
                return txbValue.Text;
            }
            set
            {
                bool flag = TestRaiseEvent(txbValue.Text, value);
                txbValue.Text = value;
                if (flag)
                {
                    OnMyTextChanged(EventArgs.Empty);
                }
            }
        }
        private bool TestRaiseEvent(string s1, string s2)
        {
            if (s1 == null)
            {
                if (s2 == null)
                    return true;
                //OnMyTextChanged(EventArgs.Empty);
                //return;
            }
            if (s2 == null)
            {
               // OnMyTextChanged(EventArgs.Empty);
                return true;
            }
            if (s1 == s2)
                return false;
            return true;
           // OnMyTextChanged(EventArgs.Empty);
        }
        public event EventHandler MyTextChanget
        {
            add
            {
                this.Events.AddHandler(objTextChangetKey, value);
            }
            remove
            {
                this.Events.RemoveHandler(objTextChangetKey, value);
            }
        }
        public event EventHandler MySelectButtonClick
        {
            add
            {
                this.Events.AddHandler(objSelectButtonClick, value);
            }
            remove
            {
                this.Events.RemoveHandler(objSelectButtonClick, value);
            }
        }
        protected void OnMySelectButtonClick(EventArgs args)
        {
            EventHandler d = this.Events[objSelectButtonClick] as EventHandler;
            if (d != null)
            {
                d(this, args);
            }
        }
        protected void OnMyTextChanged(EventArgs args)
        {
            EventHandler d = Events[objTextChangetKey] as EventHandler;
            if (d != null)
            {
                d(this, args);
            }
        }
        public Selecter()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OnMySelectButtonClick(EventArgs.Empty);
        }
    }
}
